﻿using Microsoft.AspNetCore.Http;
using Senparc.Weixin.MP.TenPayLibV3;
using System;
using System.Collections.Generic;
using System.Text;

namespace FwProject.WxPay
{
  public static class TenPayV3UnifiedorderRequestDataExtensions
    {
        public static RequestHandler PackageRequestHandlerParsXML(this TenPayV3UnifiedorderRequestData services, HttpContext httpContext) {

            RequestHandler PackageRequestHandler = new RequestHandler(httpContext);
            //初始化
            PackageRequestHandler.Init();

            PackageRequestHandler.SetParameter("appid", services.AppId);                       //公众账号ID
            PackageRequestHandler.SetParameter("mch_id", services.MchId);                      //商户号
            PackageRequestHandler.SetParameterWhenNotNull("sub_appid", services.SubAppId);     //子商户公众账号ID
            PackageRequestHandler.SetParameterWhenNotNull("sub_mch_id", services.SubMchId);    //子商户号
            PackageRequestHandler.SetParameterWhenNotNull("device_info", services.DeviceInfo); //自定义参数
            PackageRequestHandler.SetParameter("nonce_str", services.NonceStr);                //随机字符串
            PackageRequestHandler.SetParameterWhenNotNull("sign_type", services.SignType);     //签名类型，默认为MD5
            PackageRequestHandler.SetParameter("body", services.Body);                         //商品信息
            PackageRequestHandler.SetParameterWhenNotNull("detail", services.Detail);          //商品详细列表
            PackageRequestHandler.SetParameterWhenNotNull("attach", services.Attach);          //附加数据
            PackageRequestHandler.SetParameter("out_trade_no", services.OutTradeNo);           //商家订单号
            PackageRequestHandler.SetParameterWhenNotNull("fee_type", services.FeeType);       //符合ISO 4217标准的三位字母代码，默认人民币：CNY
            PackageRequestHandler.SetParameter("total_fee", services.TotalFee.ToString());     //商品金额,以分为单位(money * 100).ToString()
            PackageRequestHandler.SetParameter("spbill_create_ip", services.SpbillCreateIP);   //用户的公网ip，不是商户服务器IP
            PackageRequestHandler.SetParameterWhenNotNull("time_start", services.TimeStart);   //订单生成时间
            PackageRequestHandler.SetParameterWhenNotNull("time_expire", services.TimeExpire);  //订单失效时间
            PackageRequestHandler.SetParameterWhenNotNull("goods_tag", services.GoodsTag);     //商品标记
            PackageRequestHandler.SetParameter("notify_url", services.NotifyUrl);              //接收财付通通知的URL
            PackageRequestHandler.SetParameter("trade_type", services.TradeType.ToString());   //交易类型
            PackageRequestHandler.SetParameterWhenNotNull("product_id", services.ProductId);   //trade_type=NATIVE时（即扫码支付），此参数必传。
            PackageRequestHandler.SetParameterWhenNotNull("limit_pay", services.LimitPay);     //上传此参数no_credit--可限制用户不能使用信用卡支付
            PackageRequestHandler.SetParameterWhenNotNull("openid", services.OpenId);                     //用户的openId，trade_type=JSAPI时（即公众号支付），此参数必传
            PackageRequestHandler.SetParameterWhenNotNull("sub_openid", services.SubOpenid);              //用户子标识
            string  Sign = PackageRequestHandler.CreateMd5Sign("key", services.Key);
            PackageRequestHandler.SetParameter("sign", Sign);

            return PackageRequestHandler;
        }
    }
}
